package com.scu.controller;

import com.scu.pojo.PageInfo;
import com.scu.pojo.User;
import com.scu.pojo.UserRank;
import com.scu.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("getAllUser")
    @ResponseBody
    public HashMap getAllUser(PageInfo info) {
        List<User> userList = adminService.getAllUser(info);
        int count = adminService.getUserNum();
        HashMap<String,Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", userList);
        return map;
    }

    @RequestMapping("getUserRank")
    @ResponseBody
    public HashMap getUserRank() {
        List<UserRank> userRankList = adminService.getUserRank();
        HashMap<String,Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "");
        map.put("data", userRankList);
        return map;
    }

    @RequestMapping("getNewUser")
    @ResponseBody
    public HashMap getNewUser() {
        List<User> newUserList = adminService.getNewUser();
        HashMap<String,Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "");
        map.put("data", newUserList);
        return map;
    }

    @RequestMapping("banUser")
    @ResponseBody
    public HashMap banUser(int u_id) {
        int result = adminService.banUser(u_id);
        HashMap<String,Object> map = new HashMap<>();
        if(result == 1){
            map.put("code", 200);
            map.put("msg", "");
        } else {
            map.put("code", 500);
            map.put("msg", "禁止用户失败！");
        }
        return map;
    }

    @RequestMapping("changeUserType")
    @ResponseBody
    public HashMap changeUserType(int u_id) {
        int result = adminService.changeUserType(u_id);
        HashMap<String,Object> map = new HashMap<>();
        if(result == 1){
            map.put("code", 200);
            map.put("msg", "");
        } else {
            map.put("code", 500);
            map.put("msg", "更改失败！");
        }
        return map;
    }

    @RequestMapping("deleteUser")
    @ResponseBody
    public HashMap deleteUser(int u_id) {
        int result = adminService.deleteUser(u_id);
        HashMap<String,Object> map = new HashMap<>();
        if(result == 1){
            map.put("code", 200);
            map.put("msg", "");
        } else {
            map.put("code", 500);
            map.put("msg", "删除失败！");
        }
        return map;
    }

    @RequestMapping("getUserNum")
    @ResponseBody
    public HashMap getUserNum(){
        int count = adminService.getUserNum();
        HashMap<String,Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "");
        map.put("count", count);
        return map;
    }
}
